package com.skd.mall.cart.controller;


import com.skd.common.constant.AuthConstant;
import com.skd.common.vo.MemberVO;
import com.skd.mall.cart.interceptor.AuthInterceptor;
import com.skd.mall.cart.service.ICartService;
import com.skd.mall.cart.vo.Cart;
import com.skd.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    private ICartService cartService;


    @RequestMapping("/getCartlist")
    public String queryCartList(Model model){
        Cart cart = cartService.getCartList();
        model.addAttribute("cart",cart);
        return "/cartList";
    }

    /**
     * 删除商品
     * @param
     * @return
     */
    @RequestMapping("/deleteCart/{skuId}")
    public String deleteCar(@PathVariable("skuId") Long skuId, RedirectAttributes redirectAttributes){
        cartService.deleteCartList(skuId);
        redirectAttributes.addFlashAttribute("message", "已删除");
        return "redirect:http://cart.skd.com/getCartlist";
    }

    @RequestMapping("/addCart")
    public String addCart(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num, Model model) throws ExecutionException, InterruptedException {
        CartItem cartItem = null;
        try {
            cartItem = cartService.addCart(skuId, num);
        }catch (ExecutionException e){
            e.printStackTrace();
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        model.addAttribute("item",cartItem);
        //System.out.println(skuId);
        //System.out.println(num);
        return "/success";
    }

    @RequestMapping("/{memberId}/getUserCartItem")
    @ResponseBody
    public List<CartItem> getUserCartItem(@PathVariable("memberId") Long memberId){
        List<CartItem> list = cartService.getUserCartItem();
        return list;
    }

    @RequestMapping("/updateCartList")
    public void updateCartList(@RequestBody List<Long> skuIdList){
        cartService.updateCartList(skuIdList);
    }

    @ResponseBody
    @RequestMapping("/updateCartLists")
    public void updateCartLists(@RequestBody List<Long> skuIdList){
        cartService.updateCartList(skuIdList);
    }


}
